package com.qf.web.controller;

import com.qf.domain.PageBean;
import com.qf.domain.Stu;
import com.qf.service.StuService;
import com.qf.service.impl.StuServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * projectName:lipengchong
 * author:LiPengChong
 * time:2024/4/30 11:21
 * description:
 */
@WebServlet("/userservlet")
public class UserServlet extends BaseServlet {
    public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取前端数据
        String stuname = request.getParameter("stuname");
        String pwd = request.getParameter("pwd");
        //2.非空验证
        if (StringUtils.isEmpty(pwd)) {
            request.setAttribute("msg", "用户名不能为空");
            return "/login.jsp";
        }
        if (StringUtils.isEmpty(pwd)) {
            request.setAttribute("msg", "密码不能为空");
            return "/login.jsp";
        }
        try {
            //3.调用业务逻辑
            StuService stuService = new StuServiceImpl();
            Stu stu = stuService.login(stuname, pwd);
            if (stu.getStatus() == 1) {
                //转发重定向
                request.setAttribute("stu", stu);
                return "/adminIndex.jsp";
            } else {
                //转发重定向
                request.setAttribute("stu", stu);
                return "/yongHuIndex.jsp";
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "用户登录失败" + e.getMessage());
            return "/login.jsp";
        }
    }


}
